import java.awt.GridLayout;
import java.lang.reflect.Field;

import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import mover.*;

/**
 * a panel for the instance variables in SpringAndMassParameters
 * @author ga-youngjoung
 *
 */
public class SpringAndMassParametersPanel extends JPanel{
	
	private SpringAndMassParameters myParameters;
	
	public SpringAndMassParametersPanel() {
		myParameters = new SpringAndMassParameters();
	}
	
	/**
     * ChangeListener to set a certain instance variable in Parameters
     * variables must be a double
     * @author ga-youngjoung
     *
     */
    public class ParameterChangeListener implements ChangeListener {
    	
    	private String myParameterToBeChanged;
    	
    	public ParameterChangeListener(String parameterToBeChanged) {
    		myParameterToBeChanged = parameterToBeChanged;
    	}
    	
		public void stateChanged(ChangeEvent e) {
			JSlider source = (JSlider)e.getSource();
	        if (!source.getValueIsAdjusting()) {
	            double newParameterValue = (double)source.getValue();
	            try {
					Field parameterToChange = myParameters.getClass().getField(myParameterToBeChanged);
					parameterToChange.setDouble(myParameters, newParameterValue);
				} catch (SecurityException e0) {
					System.err.println(e0);
				} catch (NoSuchFieldException e1) {
					System.err.println(e1);
				} catch (IllegalArgumentException e2) {
					System.err.println(e2);
				} catch (IllegalAccessException e3) {
					System.err.println(e3);
				}
	        }  
		}
    	
    }
    
    /**
     * adds the slider for the Parameter value along with its appropriate Change Listener to the Panel
     * @param parameterToBeChanged
     * @param title to be shown on the GUI
     * @param minValue of the RangeSlider
     * @param maxValue of the RangeSlider
     * @param setValue of the RangeSlider
     * @param spacing of the RangeSlider
     */
    public void addParameterSlider(String parameterToBeChanged, String title, int minValue, int maxValue, int setValue, int spacing) {
    	RangeSlider slider = new RangeSlider(title, minValue, maxValue, setValue, spacing);
    	slider.addChangeListener(new ParameterChangeListener(parameterToBeChanged));
    	add(slider);
    }
}
